# 定义辅助的工具函数
from langgraph.graph import StateGraph



def save_graph_visualization(graph: StateGraph, filename: str = "graph.png") -> None:
    """将构建的graph可视化保存为 PNG 文件.

    Args:
        graph: 必须是compile()后的图，否则没有get_graph()方法.
        fiename: 要保存的png图路径
    """
    try:
        with open(filename, "wb") as f:
            f.write(graph.get_graph().draw_mermaid_png())
    except IOError as e:
        print(f"save {filename} error.")

